package com.vcredit.creditcat.service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.vcredit.creditcat.enumtype.auth.SupplementIemCategoryEnum;
import com.vcredit.creditcat.model.Cust;
import com.vcredit.creditcat.model.CustSupplementItem;
import com.vcredit.creditcat.model.ProcessSpecialItem;
import com.vcredit.creditcat.model.ProductSupplementItem;
import com.vcredit.creditcat.persistence.base.CustSupplementItemMapper;
import com.vcredit.creditcat.service.ext.ProcessSpecialItemExtService;
import com.vcredit.creditcat.service.speedloan.ProcessSpecialItemService;
import com.vcredit.creditcat.util.JsonUtil;
import com.vcredit.creditcat.util.Utility;
import com.vcredit.creditcat.vo.auth.AuthMobileExt;
import com.vcredit.creditcat.vo.auth.AuthPersonalInfoExt;
import com.vcredit.creditcat.vo.auth.supplement.CustSupplementItemExt;
import com.vcredit.framework.annotation.Log;
import com.vcredit.framework.exception.BaseException;
import com.vcredit.framework.util.BeanUtil;
/**
 * 
 **/
@Service
public class CustSupplementItemService {
	
	@Autowired
	private ProductSupplementItemService productSupplementItemService;
	@Autowired
	private AuthPersonalInfoService authPersonalInfoService;
	@Autowired
	private AuthMobileService authMobileService;
	@Autowired
	private ProcessSpecialItemExtService processSpecialItemExtService;
	@Autowired
	private ProcessSpecialItemService processSpecialItemService;
	
	@Autowired
    private CustSupplementItemMapper mapper;
    
    public void insert(CustSupplementItem obj){
    	mapper.insert(obj);
    }
    
    public void update(CustSupplementItem obj){
    	mapper.update(obj);
    }
    
    public List<CustSupplementItem> findAll(){
		return mapper.findAll();
	}
	
	public List<CustSupplementItem> find(CustSupplementItem obj){
		return mapper.find(obj);
	}

	public List<CustSupplementItem>  findAllItemsByCustAndProduct(Cust cust, long productId) throws JsonProcessingException {
		List<CustSupplementItemExt> allItemExt = this.findAllItemExtsByCustAndProduct(cust, productId);
		List<CustSupplementItem> result = new ArrayList<CustSupplementItem>();
		for(CustSupplementItemExt custSupplementItemExt : allItemExt) {
			result.add(custSupplementItemExt.getCustSupplementItem());
		}
		return result;
	}
	
	/**
	 * 查询用户对该产品的补充认证项的认证信息
	 * @author zhaoqi01
	 * @param cust
	 * @param productId
	 * @return
	 * @throws JsonProcessingException
	 */
	public List<CustSupplementItemExt> findAllItemExtsByCustAndProduct(Cust cust, long productId) throws JsonProcessingException {
		List<CustSupplementItemExt> result = new ArrayList<CustSupplementItemExt>();
		// 根据产品id查询快贷流程配置
		List<ProcessSpecialItem> processSpecialItems = processSpecialItemExtService.findByProductAndType(productId, 1);
		if(Utility.isNullOrEmpty(processSpecialItems)) {
			return result;
		}

		Map<Long, ProductSupplementItem> supplementItemMap = productSupplementItemService.findAllForMap();
		for (ProcessSpecialItem processSpecialItem : processSpecialItems) {
			ProductSupplementItem productSupplementItem = supplementItemMap.get(processSpecialItem.getItemId());
			CustSupplementItem custSupplementItem = this.findByCustAndItemId(cust.getCustId(), productSupplementItem.getItemId());
			if(null == custSupplementItem) {
				custSupplementItem = new CustSupplementItem();
				custSupplementItem.setCustId(cust.getCustId());
				custSupplementItem.setSupplementItemId(productSupplementItem.getItemId());
				custSupplementItem.setStatus(0);
			}
			this.setExt(custSupplementItem, productSupplementItem, cust);
			result.add(new CustSupplementItemExt(custSupplementItem, productSupplementItem));
		}
		return result;
	}

	public List<CustSupplementItemExt> findItemExtsByCustAndProduct(Cust cust, long productId){
		List<CustSupplementItemExt> result = new ArrayList<CustSupplementItemExt>();
		// 根据产品id查询快贷流程配置
		List<ProcessSpecialItem> processSpecialItems = processSpecialItemExtService.findByProductAndType(productId, 1);
		if(Utility.isNullOrEmpty(processSpecialItems)) {
			return result;
		}
		Map<Long, ProductSupplementItem> supplementItemMap = productSupplementItemService.findAllForMap();
		for (ProcessSpecialItem processSpecialItem : processSpecialItems) {
			ProductSupplementItem productSupplementItem = supplementItemMap.get(processSpecialItem.getItemId());
			CustSupplementItem custSupplementItem = this.findByCustAndItemId(cust.getCustId(), productSupplementItem.getItemId());
			if(custSupplementItem == null) {
				custSupplementItem = new CustSupplementItem();
				custSupplementItem.setCustId(cust.getCustId());
				custSupplementItem.setSupplementItemId(productSupplementItem.getItemId());
				custSupplementItem.setStatus(0);
			}
			result.add(new CustSupplementItemExt(custSupplementItem, productSupplementItem));
		}
		return result;
	}
	
	private CustSupplementItem findByCustAndItemId(Long custId, Long itemId) {
		if(custId == null || itemId == null) {
			return null;
		}
		CustSupplementItem param = new CustSupplementItem();
		param.setCustId(custId);
		param.setSupplementItemId(itemId);
		List<CustSupplementItem> list = mapper.find(param);
		if(Utility.isNullOrEmpty(list)) {
			return null;
		}
		// 解决find查询出来的list是一个缓存对象
		CustSupplementItem result = new CustSupplementItem();
		CustSupplementItem custSupplementItem = list.get(0);
		BeanUtil.copy(custSupplementItem, result);
		return result;
	}
	
	private void setExt(CustSupplementItem custSupplementItem,
			ProductSupplementItem productSupplementItem, Cust cust) throws JsonProcessingException {
		int supplementCategoryId = productSupplementItem.getSupplementCategoryId().intValue();
		// 将原认证项的基本信息数据设置进补充项数据中
		if(SupplementIemCategoryEnum.BASE_INFO.getCategory() == supplementCategoryId) {
			AuthPersonalInfoExt personalInfoExt = authPersonalInfoService.findExtByCust(cust);
			custSupplementItem.setDataEntity(JsonUtil.obj2Json(personalInfoExt, true));
		} 
		else if(SupplementIemCategoryEnum.MOBILE.getCategory() == supplementCategoryId) {
			AuthMobileExt mobileExt=null;
			if(custSupplementItem.getDataEntity()==null){
				mobileExt=new AuthMobileExt();
			}else{
				mobileExt = authMobileService.findExtById(Long.parseLong(custSupplementItem.getDataEntity()));
				custSupplementItem.setDataEntity(JsonUtil.obj2Json(mobileExt, true));
			}
		}
	}
	
	/**
	 * 补充认证项完成
	 * @author zhaoqi01
	 * @param cust
	 * @throws BaseException
	 */
	@Log
	public boolean itemDone(Cust cust, Integer itemCode, String dataId, String expireTime) throws BaseException {
		return this.saveOrUpdateStatus(cust, itemCode, dataId, 1, expireTime);
	}
	
	/**
	 * 补充认证项完成
	 * @author zhaoqi01
	 * @param cust
	 * @param productId
	 * @param category
	 * @return
	 * @throws BaseException
	 */
	@Log
	public boolean itemDone(Cust cust, Long productId , Integer category, String dataId, String expireTime) throws BaseException {
		return this.saveOrUpdateStatus(cust, productId, category, dataId, 1, expireTime);
	}
	
	/**
	 * 保存或更新补充认证项的认证的各个状态
	 * @author zhaoqi01
	 * @param cust
	 * @param productId
	 * @param category
	 * @param status
	 * @return
	 * @throws BaseException
	 */
	@Log
	public boolean saveOrUpdateStatus(Cust cust, Long productId , Integer category, String dataId, Integer status, String expireTime) throws BaseException {
		if(!(status == 0 || status == 1 || status == 2 || status == 3)) {
			throw new BaseException("参数错误");
		}
		// 根据productId查询快贷流程项
		List<ProcessSpecialItem> processSpecialItems = processSpecialItemService.findByProductId(productId);
		for (ProcessSpecialItem processSpecialItem : processSpecialItems) {
			if(processSpecialItem.getItemType() == 0)
				continue;
			// 根据supplementId查询对应的补充项配置
			ProductSupplementItem supplementItem = productSupplementItemService.findById(processSpecialItem.getItemId());
			if(null == supplementItem) {
				return false;
			}
			if(supplementItem.getSupplementCategoryId() == category) {
				CustSupplementItem custSupplementItem = new CustSupplementItem();
				custSupplementItem.setCustId(cust.getCustId());
				custSupplementItem.setSupplementItemId(supplementItem.getItemId());
				List<CustSupplementItem> list = this.find(custSupplementItem);
				if(Utility.isNullOrEmpty(list)) { // 新增
					custSupplementItem.setProccessId(cust.getLastProccessId());
					custSupplementItem.setDataEntity(dataId);
					custSupplementItem.setStatus(status);
					custSupplementItem.setExpireTime(expireTime);
					this.insert(custSupplementItem);
				}
				else { // 更新
					custSupplementItem = list.get(0);
					custSupplementItem.setDataEntity(dataId);
					custSupplementItem.setStatus(status);
					custSupplementItem.setExpireTime(expireTime);
					this.update(custSupplementItem);
				}
				
				return true;
			}
			
		}
		return false;
	}

	/**
	 * 保存或更新补充认证项的认证的各个状态
	 * @author zhaoqi01
	 * @param cust
	 * @param status
	 * @return
	 * @throws BaseException
	 */
	@Log
	public boolean saveOrUpdateStatus(Cust cust, Integer itemCode, String dataId, Integer status, String expireTime) throws BaseException {
		if(!(status == 0 || status == 1 || status == 2 || status == 3)) {
			throw new BaseException("参数错误");
		}
		ProductSupplementItem productSupplementItem = new ProductSupplementItem();
		productSupplementItem.setItemCode(itemCode);
		List<ProductSupplementItem> productSupplementItems = productSupplementItemService.find(productSupplementItem);
		if(Utility.isNullOrEmpty(productSupplementItems))
			return true;

		productSupplementItem = productSupplementItems.get(0);

		CustSupplementItem custSupplementItem = new CustSupplementItem();
		custSupplementItem.setCustId(cust.getCustId());
		custSupplementItem.setSupplementItemId(productSupplementItem.getItemId());
		List<CustSupplementItem> list = this.find(custSupplementItem);
		if(Utility.isNullOrEmpty(list)) { // 新增
			custSupplementItem.setProccessId(cust.getLastProccessId());
			custSupplementItem.setDataEntity(dataId);
			custSupplementItem.setStatus(status);
			custSupplementItem.setExpireTime(expireTime);
			this.insert(custSupplementItem);
		}
		else { // 更新
			custSupplementItem = list.get(0);
			custSupplementItem.setDataEntity(dataId);
			custSupplementItem.setStatus(status);
			custSupplementItem.setExpireTime(expireTime);
			this.update(custSupplementItem);
		}

		return true;
	}

	public void resetItem(Long id) {
		CustSupplementItem item = new CustSupplementItem();
		item.setId(id);
		item.setStatus(0);
		mapper.update(item);
	}

	public CustSupplementItemExt findExtByCustAndItemCode(Cust cust,
			Integer authItemCodeIntVal) {
		CustSupplementItem custSupplementItem = mapper.findByCustAndItemCode(cust.getCustId(), authItemCodeIntVal);
		ProductSupplementItem productSupplementItem = productSupplementItemService.findByItemCode(authItemCodeIntVal);
		if(null == custSupplementItem) {
			custSupplementItem = new CustSupplementItem();
			custSupplementItem.setCustId(cust.getCustId());
			custSupplementItem.setSupplementItemId(productSupplementItem.getItemId());
			custSupplementItem.setStatus(0);
		}
		return new CustSupplementItemExt(custSupplementItem, productSupplementItem);
	}

	public CustSupplementItem findByCustAndItemCode(Cust cust,
			Integer authItemCodeIntVal) {
		return mapper.findByCustAndItemCode(cust.getCustId(), authItemCodeIntVal);
	}

	public CustSupplementItem findByCustAndSupplementItemId(Long custId,
			Long itemId) {
		Map<Long, ProductSupplementItem> allSupplementItem = productSupplementItemService.findAllForMap();
		ProductSupplementItem supplementItem = allSupplementItem.get(itemId);
		return mapper.findByCustAndItemCode(custId, supplementItem.getItemCode());
	}
}